//
//  DropLoadingView.m
//  MVideo
//
//  Created by Lin Eidan on 13-12-6.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "DropLoadingView.h"
#import "UIView+EidanSize.h"

#define ScrollViewShouldScrllOffsetY        50
#define CanUnClasp                          @"松开更新"
#define CanUnClaspImageName                 @""

#define HardLoading                         @"更新中..."

#define CanLoadingMore                      @"下拉更新"
#define CanLoadingMoreImageName             @""


@implementation DropLoadingView

- (id)initWithParentView:(UIScrollView *)scrollView andDelegate:(id)delegate
{
    self = [super init];
    if (self) {
        self.parentView = scrollView;
        self.delegate = delegate;
        self.defaultFrameY = 40;
        [self setUp];
    }
    return self;
}

-(void)setUp{    
    [[NSBundle mainBundle] loadNibNamed:@"DropLoadingView" owner:self options:nil];
    self.width=self.customView.width;
    self.height=self.customView.height;
    [self addSubview:self.customView];
    self.left = (self.parentView.width - self.width)/2;
    self.top = - self.defaultFrameY;
    [self.parentView addSubview:self];
}

-(void)handleStateNormal{
    self.dropState = DropNormal;
    self.loadingActView.hidden = YES;
    self.loadingLabel.text = CanLoadingMore;
    self.loadingImageView.image = [UIImage imageNamed:CanLoadingMoreImageName];
    self.loadingImageView.hidden = NO;
}

-(void)handleStateReadyLoad{
    self.dropState = DropReadyLoad;
    self.loadingActView.hidden = YES;
    self.loadingLabel.text = CanUnClasp;
    self.loadingImageView.image = [UIImage imageNamed:CanUnClaspImageName];
}

-(void)handleStateStartLoad{
    if (self.dropState == DropReadyLoad) {  //如果是准备加载的话那么。。
        self.dropState = DropLoading;
        self.loadingActView.hidden = NO;
        self.loadingLabel.text = HardLoading;
        self.loadingImageView.hidden= YES;
        
        [UIView animateWithDuration:0.4 animations:^{
            [self.parentView setContentOffset:CGPointMake(0, -self.defaultFrameY)];  //以动画的方式到那个位置，，但是到这这个位置后，其还会自动到底，所以上面还要处理。。
        } completion:^(BOOL finished) {
            
            if ([self.delegate respondsToSelector:@selector(dropLoadingViewStartGetData:)]) {
                [self.delegate dropLoadingViewStartGetData:self];
            }
        }];
    }
    
}

-(void)onScroll{
    float offsetY = - self.parentView.contentOffsetTop - ScrollViewShouldScrllOffsetY;
    
    if (self.dropState != DropLoading && self.dropState != DropLoadedSuccess) {
        if (offsetY < 0) {
            [self handleStateNormal];
        }else{
            [self handleStateReadyLoad];
        }
    }else if(self.dropState == DropLoading){  //正在加载中，保持位置
        if (self.parentView.contentOffsetTop > -self.defaultFrameY) {
            self.parentView.contentOffset=CGPointMake(0, -self.defaultFrameY);
        }
        
    }
    
    
}

-(void)endDraing{
    [self handleStateStartLoad];
    
    //放开后，如果是获得数据了，那么就归位。
    if (self.dropState == DropLoadedSuccess) {
        [self handleStateNormal];
        [self.parentView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}



-(void)hasGetDataOverAndRestore{
    self.dropState = DropLoadedSuccess;
    //看是否在拖拽着。如果是的话，也不能归位。
    if (!self.parentView.isDragging) {
        [self handleStateNormal];
        [self.parentView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

-(void)hasCancelRequestAndRestore{
    BOOL isToDefault = YES;
    if (self.dropState == DropNormal) {
        isToDefault = NO;
    }
    self.dropState = DropLoadedSuccess;
    //看是否在拖拽着。如果是的话，也不能归位。
    if (!self.parentView.isDragging) {
        [self handleStateNormal];
        if (isToDefault) {
            [self.parentView setContentOffset:CGPointMake(0, 0) animated:YES];
        }
    }
}


@end
